package com.example.myapplication.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.adapter.News02Adapter;
import com.example.myapplication.adapter.NewsAdapter;
import com.example.myapplication.data.NewBannerData;
import com.example.myapplication.data.NewFenLeiData;
import com.example.myapplication.data.NewsSort;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class NewsFragment extends Fragment {

    private View view;
    private ViewPager banner;
    private TabLayout news;
    private Handler handler;
    private RecyclerView arrange;
    private List<NewFenLeiData.DataDTO> listFenLei;
    private List<NewBannerData.RowsDTO> getArrange;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_news, container, false);
        init();
        getBanner();
        getNewsSort();
        getArrange(9);
        return view;
    }
    private void getArrange(int type ) {
        String url = "prod-api/press/press/list?type="+type;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NewBannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NewBannerData newBan = (NewBannerData)obj;
                if(newBan.getCode() == 200){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            getArrange = newBan.getRows();
                            arrangeAdapter();
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void arrangeAdapter() {
        News02Adapter newsAdapter = new News02Adapter(getArrange,NewsFragment.this.getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        arrange.setLayoutManager(linearLayoutManager);
        arrange.setAdapter(newsAdapter);
    }

    private void getNewsSort() {

        String url = "prod-api/press/category/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NewFenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NewFenLeiData newsSort = (NewFenLeiData)obj;
                listFenLei = newsSort.getData();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (NewFenLeiData.DataDTO  dataDTO: listFenLei) {
                            TabLayout.Tab tab = news.newTab();
                            tab.setText(dataDTO.getName());
                            news.addTab(tab);
                        }
                        newsSortAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void newsSortAdapter() {
        news.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                int id = listFenLei.get(position).getId();
                getArrange(id);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    private void getBanner() {
        String url = "prod-api/press/press/list?pageNum=1&pageSize=3";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, NewBannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                NewBannerData newBan = (NewBannerData)obj;
                ArrayList<String> list = new ArrayList<>();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (NewBannerData.RowsDTO row : newBan.getRows()) {
                            list.add(row.getCover());
                        }
                        BannerTool bannerTool = new BannerTool(NewsFragment.this.getContext(),list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void init() {
        banner = view.findViewById(R.id.banner);
        arrange = view.findViewById(R.id.arrange);
        news = view.findViewById(R.id.news);
        handler = new Handler();
    }
}